/*
 * 自己的JS脚步
 * @Author: iceStone
 * @Date:   2015-12-12 10:59:26
 * @Last Modified by:   iceStone
 * @Last Modified time: 2015-12-12 11:01:38
 */

'use strict';

$(function () {
    $(window).on('resize', resize).trigger('resize');
    function resize() {
        var windowWidth = $(window).width();
        $('#loop-view-pager>.carousel-inner > .item').each(function (index, ele) {
            $(ele).empty();
            if (windowWidth < 768) {
                $(ele).html('<img src="' + $(ele).data("img-xs") + '" alt="">').css('background-image', '');
            } else {
                $(ele).css('background-image', 'url(' + $(ele).data('img-lg') + ')');
            }
        })

        var $ulContainer = $('#products ul.nav-tabs');
        var sumWidth = 0;
        $ulContainer.children().each(function (index, ele) {
            sumWidth += ele.offsetWidth;
        });
        if ($ulContainer.parent()[0].offsetWidth<sumWidth) {
            $ulContainer.width(sumWidth);
            $ulContainer.parent().css('overflow-x','scroll');
        }else {
            $ulContainer.parent().css('overflow-x','hidden');
        }
    }

    $('[data-toggle="tooltip"]').tooltip();


    $("#news>.container .nav-pills>li>a").on('click',function () {
        var title = $(this).data('title');
        $("#news .news-title").html(title);
    })


    /**
     * 解决移动端手指滑动问题
     */
    var startX,endX;
    $('#main_ad>#loop-view-pager').on('touchstart',function (e) {
        startX = e.originalEvent.touches[0].clientX;
    })
    $('#main_ad>#loop-view-pager').on('touchmove',function (e) {
        endX = e.originalEvent.touches[0].clientX;
    });

    $('#main_ad>#loop-view-pager').on('touchend',function (e) {
        if(Math.abs(startX - endX)>50){
            $(this).carousel(startX>endX?'next':'prev');
        }
    });


});